package org.adl.registry.types;

import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import net.handle.hdllib.HandleException;
import net.handle.hdllib.HandleValue;
import org.adl.registry.handles.HandleUtil;

/**
 *
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class ClientAccessKey extends RimParameter
{
    public static final String CAK_NAMESPACE = "2000.2.4";
    
    private static final String HS_ADMIN = "HS_ADMIN";
    private static final String EMAIL = "EMAIL";
    private static final String DESC = "DESC";
    private static final String IP = "100.TYPES/CORDRA.CONFIG.RIM.CLIENT.IP";
    
    private Handle mHandle;
    
    private String mEmail;
    private String mDesc;
    private String mClientIP;
    
    public ClientAccessKey(String iHandle)
    {
        super("cak", iHandle);
        
        this.validate();  
    }
    
    public ClientAccessKey(String iHandle, String iEmail, String iDescription, String iIp)
    {
        this(iHandle);
        
        this.setEmail(iEmail);
        this.setDesc(iDescription);
        this.setClientIP(iIp);
    }
    
//    public ClientAccessKey(String iEmail, String iDescription, String iIp)
//    {
//        super("cak", ClientAccessKey.getNextCAKHandle());
//
//        this.setEmail(iEmail);
//        this.setDesc(iDescription);
//        this.setClientIP(iIp);
//    }

//    public ClientAccessKey(Handle iHandle)
//    {
//        this.mHandle = iHandle;
//        
//        Handle handle = new Handle(iHandle);
//        
//        HandleValue[] values = handle.getHandleValues();
//        
//        for (int i = 0; i < values.length; i++)
//        {
//            HandleValue value = values[i];
//            
//            String type = value.getTypeAsString();
//            
//            if (type.equalsIgnoreCase(ClientAccessKey.IP))
//            {
//                cak.setClientIP(value.getDataAsString());
//            }
//            else if (type.equalsIgnoreCase(ClientAccessKey.DESC))
//            {
//                cak.setDesc(value.getDataAsString());
//            }
//            else if (type.equalsIgnoreCase(ClientAccessKey.EMAIL))
//            {
//                cak.setEmail(value.getDataAsString());
//            }
//            else
//            {
//                List<HandleValue> other = cak.getMOtherValues();
//                
//                other.add(value);
//            }
//        }
//    }
    
    public String getEmail()
    {
        return this.mEmail;
    }
    
    public void setEmail(String iEmail)
    {
        this.mEmail = iEmail;
    }
    
    public String getDesc()
    {
        return this.mDesc;
    }
    
    public void setDesc(String iDesc)
    {
        this.mDesc = iDesc;
    }
    
    public String getClientIP()
    {
        return this.mClientIP;
    }
    
    public void setClientIP(String iClientIP)
    {
        this.mClientIP = iClientIP;
    }
    
    public Handle getHandle()
    {
        return this.mHandle;
    }
    
    public void setHandle(Handle iHandle)
    {
        this.mHandle = iHandle;
    }
    
    public static Boolean authenticate(String iHandle, HttpServletRequest iRequest)
    throws FileNotFoundException, IOException, HandleException
    {
        Boolean validated = false;
        
        HandleValue[] values = HandleUtil.resolveHandle(iHandle, null, null);
        
        String ip = "";
        
        for (int i = 0; i < values.length; i++)
        {
            HandleValue value = values[i];
            
            String type = value.getTypeAsString();
            
            if (type.equalsIgnoreCase("100.TYPES/CORDRA.CONFIG.RIM.CLIENT.IP"))
            {
                ip = value.getDataAsString();
                
                break;
            }
        }
        
        if (iRequest.getRemoteAddr().equalsIgnoreCase(ip))
        {
            validated = true;
        }
        
        return validated;
    }
    
//    public static String getNextCAKHandle()
//    {
//        String next = "";
//        
//        //TODO: Generate New Handle Value. Will require Ability to list 2000.2.4 namespace.
//        
//        return next;
//    }
    
    protected void validate()
    {
        //TODO: validate
    }
    
    public boolean exists()
    {
        return this.mHandle.getHandleExists();
    }
}
